﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

public partial class UserControl_ucJobListNonWarranty : System.Web.UI.UserControl
{
    TblJobsDAO jobDAO = new TblJobsDAO(Global.CONNECTION_STRING);
    int dem;
    int demApproval;
    int dempickup;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadJobList();
            txtdem.Text = Convert.ToString(dem);
            LoadCustomerApproval();
            txtdemapproval.Text = Convert.ToString(demApproval);
            loadcustomerpickup();
            txtdempickup.Text = Convert.ToString(dempickup);
        }
    }

    private void loadJobList()
    {
        try
        {
            grdJobList.DataSource = jobDAO.GetJobNonWarranty(1,1);
            grdJobList.DataBind();

            for (int i = 0; i <= grdJobList.Rows.Count; i++)
            {
                dem = Convert.ToInt32(grdJobList.Rows.Count);
            }
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
           
    }

    private void LoadCustomerApproval()
    {
        grdCustomerApproval.DataSource = jobDAO.GetJobNonWarranty(2,1);
        grdCustomerApproval.DataBind();

        for (int i = 0; i <= grdCustomerApproval.Rows.Count; i++)
        {
            demApproval = Convert.ToInt32(grdCustomerApproval.Rows.Count);
        }
    }
    private void loadcustomerpickup()
    {
        grdpickup.DataSource = jobDAO.GetJobNonWarranty(3,1);
        grdpickup.DataBind();

        for (int i = 0; i <= grdpickup.Rows.Count; i++)
        {
            dempickup = Convert.ToInt32(grdpickup.Rows.Count);
        }
    }

    protected void grdJobList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {           
            //Label lb_jobtype = (Label)e.Row.Cells[4].FindControl("lbjobtype");
            //int type = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "JobType"));
            //if (type == 1)
            //    lb_jobtype.Text = "Non-Warranty";             
        }
    }
   

    protected void grdCustomerApproval_RowDataBound(object sender, GridViewRowEventArgs e)
    {
    }

    protected void grdpickup_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HiddenField hidItemID = (HiddenField) e.Row.Cells[0].FindControl("hid");
            hidItemID.Value = DataBinder.Eval(e.Row.DataItem, "JobID").ToString();
            //int id = Convert.ToInt32(hidItemID.Value);
        }
    }

    protected void drStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            foreach (GridViewRow grd in grdpickup.Rows)
            {
                DropDownList drStatus = (DropDownList)grd.FindControl("drStatus");
                HiddenField hidden = (HiddenField)grd.FindControl("hid");
                string t = hidden.Value;
                int id = Global.ConvertToInt(hidden.Value);
                string value = drStatus.SelectedValue;

                jobDAO.Update(Convert.ToInt32(value), id);

                loadcustomerpickup();
            }      
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }

    }
}
